﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Kiosk.Library;
using System.Windows;

namespace Kiosk.WebManagement
{
    public partial class EditLocation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string userName = User.Identity.Name;
            EmployeeInfo emp = EmployeeInfo.GetEmployeeInfoByUserName(userName);
            Session["emp"] = emp;
            if (emp == null)
            {
                lblNotUser.Visible = true;
                marker.Visible = false;
            }
            else
            {
                lblNotUser.Visible = false;
                marker.Visible = true;
            }

            Page.PreRender += new EventHandler(Page_PreRender);
        }

        void Page_PreRender(object sender, EventArgs e)
        {
            if (Session["emp"] != null)
            {
                UpdateMarkerLocation((EmployeeInfo)Session["emp"]);
            }
        }



        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            string userName = User.Identity.Name;
            EmployeeInfo emp = EmployeeInfo.GetEmployeeInfoByUserName(userName);
            if (emp != null)
            {
                Location l = Location.GetLocation(emp.LocationID);
                l.XCoordinate = e.X;
                l.YCoordinate = e.Y;
                l.Save();
            }
        }

        private void UpdateMarkerLocation(EmployeeInfo emp)
        {

            Page.ClientScript.RegisterStartupScript(this.GetType(), "moveMarker",
                string.Format("window.onload = function() {{ setMarkerLocation({0},{1}); }}", emp.LocationPoint.X, emp.LocationPoint.Y)
                , true);
        }
    }
}
